package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jiongbull.jlog.JLog;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.Branch;
import com.sctvcloud.dongqu.beans.BranchBean;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.ui.adapter.DepartAdapter;
import com.sctvcloud.dongqu.ui.utils.GSFPlayerScreenControllImp;
import com.sctvcloud.dongqu.ui.utils.TitleUtils;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.CustomLinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DepartmentActivity extends BaseActivity implements CanRefreshLayout.OnRefreshListener,CanRefreshLayout.IScrollView {

    @BindView(R.id.rebllion_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected CustomLinearLayout detail;
    @BindView(R.id.rebllion_list)
    protected RecyclerView rebllion_list;

    @BindView(R.id.title_top_logo_layout)
    protected LinearLayout title_top_logo_layout;
    @BindView(R.id.title_top_title)
    protected CustomFontTextView title_top_title;
    @BindView(R.id.title_top_back)
    protected ImageView backImg;
    @BindView(R.id.title_top_share)
    protected CustomFontTextView title_top_share;
    private DepartAdapter mAdapter;
    private boolean mIsLoading = false;
    private int mPageNo = 0;
    private int mPageAll = 0;
    private int tag_mPageNo = 0;
    private int tag_mPageAll = 0;
    private final static int pageSize = 10;
    public static final String PIC_TAG = "dia_pic_choice_fragment";
    public static final String SHOT_TAG = "dia_shot_choice_fragment";
    public static final String EX_TYPE = "ex_type";

    private GSFPlayerScreenControllImp screenController;
    protected static final int CODE_REQUEST_LOGIN = 303;
    List<Branch> list = new ArrayList<Branch>();
    Branch branch;

    private int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        initView();

    }

    @Override
    protected void initInject() {
        setContentView(R.layout.fragment_department);
        ButterKnife.bind(this);
    }

    private void initView() {

        title_top_logo_layout.setVisibility(View.GONE);
        title_top_title.setVisibility(View.VISIBLE);
        title_top_title.setText("诉求部门");
        title_top_share.setText("完成");
        title_top_share.setVisibility(View.VISIBLE);
        title_top_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != branch) {
                    Intent intent = new Intent();
                    intent.putExtra("branchId", branch.getBranchId());
                    intent.putExtra("branchName", branch.getBranchName());
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        });


        id = getIntent().getIntExtra("branchId",0);


        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        rebllion_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rebllion_list.setItemAnimator(new DefaultItemAnimator());
        refreshLayout.autoRefresh();
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        detail.setiScrollView(this);
    }


    private  void initPos(int id) {
        for(int i=0;i<list.size();i++){
            if(list.get(i).getBranchId()==id)
            {
                branch = list.get(i);
                list.get(i).setChoose(true);
                mAdapter.notifyDataSetChanged();
                return;

            }
        }

        if(list.size()>0) {
            list.get(0).setChoose(true);
            branch = list.get(0);
            mAdapter.notifyDataSetChanged();
        }

    }


    private void getData() {
        HashMap<String, Object> param = new HashMap<>();
        NetUtils.getNetAdapter().getBranch(getOwnerName(),param, new AbsNetCallBack<BranchBean>(BranchBean.class) {
            @Override
            public void onSuc(BranchBean fRebellions) {
                if(null != fRebellions && null !=fRebellions.getBranchList() && fRebellions.getBranchList().size()>0) {
                    list = fRebellions.getBranchList();
                    initData(fRebellions.getBranchList());
                }else{
                    toast(R.string.loading_no_data);
                }

            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                toast(R.string.loading_no_data);
            }

            @Override
            public void onEnd() {
                onNetFinish();
            }
        });

    }


    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mPageNo = 0;
        mIsLoading = true;
        getData();
    }

    private void initData(List<Branch> fRebellions) {
        if (fRebellions == null || fRebellions.size() == 0) {
            fRebellions = new ArrayList<>();
        }

        if (mAdapter == null) {
            mAdapter = new DepartAdapter(this, fRebellions).setItemInternalClick(itemClick);
            rebllion_list.setAdapter(mAdapter);
        }
        if (mPageNo == 0) {
            mAdapter.setData(fRebellions);
        } else {
            mAdapter.addDatas(fRebellions);
        }
        mPageNo++;

        initPos(id);
    }

    private synchronized void onNetFinish() {
        mIsLoading = false;
        refreshLayout.refreshComplete();
        refreshLayout.loadMoreComplete();
        refreshLayout.setLoadMoreEnabled(mPageNo >= mPageAll ? false : true);
    }


    protected void toLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivityForResult(intent, CODE_REQUEST_LOGIN);
    }

    OnItemInternalClick itemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            switch (view.getId()) {
                case R.id.re_top:
                    for(int i=0;i<list.size();i++) {
                        list.get(i).setChoose(false);
                    }
                    list.get(position).setChoose(true);
                    branch = list.get(position);
                    mAdapter.notifyDataSetChanged();
                    break;

            }
        }
    };
    @Override
    public boolean canScrollUp(MotionEvent motionEvent) {
        boolean b = ViewCompat.canScrollVertically(rebllion_list, -1);
        JLog.e("ggag   " + b);
        return b;
    }

    @Override
    public boolean canScrollDown(MotionEvent motionEvent) {
        boolean b = ViewCompat.canScrollVertically(rebllion_list, 1);
        JLog.e("ggag   " + b);
        return b;
    }
}
